﻿using System;
using System.Web.UI;
using DevExpress.Web.ASPxMenu;
using System.Xml;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Common;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using SGRapidForm.Permission.Entity;
using System.Collections.Generic;
using DevExpress.Web.ASPxTreeList;

namespace SGDemo.Permission
{
    public partial class DeptList : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
            JSManager.RegAjax(Header, this.GetType().BaseType);
        }
        /// <summary>
        /// 添加快捷方式
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool SaveFastAdd()
        {
            string userkeyid = ApplicationContext.Current.UserKeyid;
            string pagecode = this.GetType().Name;
            PageEntity page = _permissionService.Page_GetKeyidBypagename(pagecode);
            if (page != null)
            {
                return _permissionService.Shortcuts_Add(userkeyid, page.Keyid);
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 菜单项绑定事件 - 实现操作权限控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void menuHeader_ItemDataBound(object sender, MenuItemEventArgs e)
        {
            IHierarchyData itemHierarchyData = (IHierarchyData)e.Item.DataItem;
            XmlElement element = (XmlElement)itemHierarchyData.Item;
        }

        //导出数据
        protected void btnExporter_Click(object sender, EventArgs e)
        {
            ASPxButton btn = sender as ASPxButton;
            string fileType = btn.CommandName;
            switch (fileType)
            {
                case "Xls": this.tleList.WriteXlsToResponse(); break;
                case "Xlsx": this.tleList.WriteXlsxToResponse(); break;
                case "Pdf": this.tleList.WritePdfToResponse(); break;
                case "Rtf": this.tleList.WriteRtfToResponse(); break;
                default: break;
            }
        }

        /// <summary>
        /// TreeList重新绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void tlList_CustomCallback(object sender, TreeListCustomCallbackEventArgs e)
        {
            tlList.DataBind();
            tlList.ExpandToLevel(2);
        }

        protected string ProcessDeptType(string deptType)
        {
            if (deptType == "0") { return "公司"; }
            if (deptType == "1") { return "普通部门"; }
            if (deptType == "2") { return "特殊部门"; }
            return string.Empty;
        }

        [AjaxPro.AjaxMethod]
        public bool Delete(string keyid)
        {
            return _permissionService.Dept_Delete(keyid);
        }
    }

    public class DeptDataSource
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        public List<Dept> FilteredGetAll(string filterConditions)
        {
            return _permissionService.Dept_FilteredGetAll(filterConditions);
        }

    }
}